#import "BWQueenStitchSet.h"
#import "BWQueenStitch.h"

@implementation BWQueenStitchSet

- (id) init
{
    if ([super init] == nil) {
        return (nil);
    }

    stitches = [[NSMutableSet alloc] init];

    return (self);

} // init


- (void) dealloc
{
    [stitches release];

    [super dealloc];

} // dealloc


- (void) addStitch: (BWQueenStitch *) stitch
{
    [stitches addObject: stitch];
} // addStitch


- (NSEnumerator *) objectEnumerator
{
    return ([stitches objectEnumerator]);
} // objectEnumerator


- (BWQueenStitch *) anyStitch
{
    return ([stitches anyObject]);

} // anyStitch


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeObject: stitches  forKey: @"stitchSet"];

} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super init]) {
        stitches = [coder decodeObjectForKey: @"stitchSet"];
        [stitches retain];
    }

    return (self);

} // initWithCoder


- (BWQueenStitch *) stitchAtX: (int) x  y: (int) y
{
    BWQueenStitch *foundStitch = nil;

    NSEnumerator *enumerator = [self objectEnumerator];

    BWQueenStitch *stitch;
    while (stitch = [enumerator nextObject]) {
        if ([stitch x] == x && [stitch y] == y) {
            foundStitch = stitch;
            break;
        }
    }

    return (foundStitch);

} // stitchAtX


- (void) removeStitch: (BWQueenStitch *) stitch
{
    if (stitch != nil) {
        [stitches removeObject: stitch];
    }

} // removeStitch


- (void) shiftStitchesHorizontally: (int) hcount
                        vertically: (int) vcount
{
    NSEnumerator *enumerator = [self objectEnumerator];

    BWQueenStitch *stitch;
    while (stitch = [enumerator nextObject]) {
        int x, y;
        x = [stitch x];
        y = [stitch y];
        [stitch setX: x + hcount];
        [stitch setY: y + vcount];
    }
  
} // shiftStitchesHorizontally

@end // BWQueenStitchSet

